package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"net/http"
	"teresa/service"
)

var Daemonset daemonset

type daemonset struct{}

func (d *daemonset) GetDaemonsets(ctx *gin.Context) {
	params := new(struct {
		FilterName string `form:"filtername"`
		Namespace  string `form:"namespace"`
		Limit      int    `form:"limit"`
		Page       int    `form:"page"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("参数绑定失败", err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败" + err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Daemonset.GetDaemonsets(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取daemonset列表成功",
		"data": data,
	})
}

func (d *daemonset) GetDaemonsetDetail(ctx *gin.Context) {
	params := new(struct {
		DaemonsetName string `form:"daemonsetname"`
		Namespace     string `form:"namespace"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("bind绑定参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败," + err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Daemonset.GetDaemonsetDetail(params.DaemonsetName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取daemonset详情成功",
		"data": data,
	})
}

func (d *daemonset) DeleteDaemonset(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `form:"namespace"`
		DaemonsetName string `form:"daemonsetname"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("bind绑定参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败," + err.Error(),
			"data": nil,
		})
		return
	}
	err := service.Daemonset.DeleteDaemonset(params.DaemonsetName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "删除daemonset成功",
		"data": nil,
	})
}

func (d *daemonset) CreateDaemonset(ctx *gin.Context) {
	var (
		daemonsetCreate = new(service.DaemonsetCreate)
		err             error
	)
	if err = ctx.ShouldBindJSON(daemonsetCreate); err != nil {
		logger.Error("bind请求参数失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败," + err.Error(),
			"data": nil,
		})
		return
	}
	if err = service.Daemonset.CreateDaemonset(daemonsetCreate); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建daemonset成功",
		"data": nil,
	})
}

func (d *daemonset) UpdateDaemonset(ctx *gin.Context) {
	// 与deploy的一样
}
